﻿unit res_manager;

interface

uses
  c_list, zglHeader;

type
  TResType = (rtFont, rtTexture);

  TResource = class
    fName: string;
    aData: Pointer;
  end;

  TResManager = class(TList)
  private
    aResDir: string;
    function LoadRes(FName: string; aType: TResType): Pointer;
  public
    function LoadFont(FName: string): zglPFont;
    function LoadTex(FName: string): zglPTexture;
    destructor Destroy; override;
    property ResDir: string read aResDir write aResDir;
  end;

implementation

function TResManager.LoadRes(FName: string; aType: TResType): Pointer;
var
  i: Integer;
  tR: TResource;
begin
  Result := nil;

  //Ищем в ресурсах
  for i := 0 to FCount - 1 do
  begin
    if TResource(FItems[i]).fName = FName then
    begin
      Result := TResource(FItems[i]).aData;
      Exit;
    end;
  end;

  //Новый ресурс
  tR := TResource.Create;
  tR.fName := FName;
  case aType of
    rtFont:    tR.aData := font_LoadFromFile(aResDir + FName);
    rtTexture: tR.aData :=  tex_LoadFromFile(aResDir + FName);
  end;
  Add(tR);
  Result := tR.aData;
end;

function TResManager.LoadFont(FName: string): zglPFont;
begin
  Result := LoadRes(FName, rtFont);
end;

function TResManager.LoadTex(FName: string): zglPTexture;
begin
  Result := LoadRes(FName, rtTexture);
end;

destructor TResManager.Destroy;
var
  i: Integer;
begin
  for i := 0 to FCount - 1 do
    TResource(FItems[i]).Free;
  inherited;
end;

end.
